<?php
namespace app\index\controller;
use think\Controller;
use app\index\controller\Common;

class Tag extends Common
{
    public function index(){	
			$id = input('id','','intval');
			if($id!=0){			
				$where['tid'] = $id;
				$aidlist = db('tagnidaid')->field('aid')->where($where)->select();
				if(count($aidlist)==0){
					 abort(404,'标签不存在');
				}
				$aids = [];
				foreach($aidlist as $Key=>$v){
					$aids[] = $v['aid'];
				}	
				$map['a.id'] = array('in',implode(',',$aids));
				$list = db('article')->alias('a')->join('nav n','n.id=a.nid')->field('a.id,a.title,a.datetime,n.entitle')->where($map)->order("a.istop desc,a.sort desc,a.id desc")->paginate(12,false,['query' => request()->param()]);				
			  $data["list"] = $list;
				$data["page"] = $list->render();	
				$tagone = db('taglist')->where(['id'=>$id])->find();
				db('taglist')->where(['id'=>$id])->setInc('views',1);
				$data["tagone"] = $tagone;
				//标签页SEO
				$data["site"]['webtitle'] = $tagone['seo_title'] == "" ? $tagone['tag'].'-'.config('webtitle') : $tagone('seo_title');
				$data["site"]['webkeywords'] = $tagone['seo_keywords'] == "" ? '' : $tagone['seo_keywords'];
				$data["site"]['webdescription'] = $tagone['seo_description'] == "" ? '' : $tagone['seo_description'];				
				$this->assign($data);			
				return $this->fetch('index@' . config('webthemes') . '/taglist');
			}else{
				$hottag = db('taglist')->order("views desc")->limit(100)->select();
				foreach($hottag as $key=>$v){
					$hottag[$key]['arcnum'] = db('tagnidaid')->where(['tid'=>$v['id']])->count();
				}
				$data['hottag'] = $hottag;
				$list = db('taglist')->order("id desc")->select();
				foreach($list as $key=>$v){
					$list[$key]['arcnum'] = db('tagnidaid')->where(['tid'=>$v['id']])->count();
				}
				$data['list'] = $list;
				//标签页SEO
				$data["site"]['webtitle'] = config('tag_seo_title') == "" ? '标签页-'.config('webtitle') : config('tag_seo_title');
				$data["site"]['webkeywords'] = config('tag_seo_keywords') == "" ? '' : config('tag_seo_keywords');
				$data["site"]['webdescription'] = config('tag_seo_description') == "" ? '' : config('tag_seo_description');
				$this->assign($data);
				return $this->fetch('index@' . config('webthemes') . '/tag');
			}
    }	
	
}
